package com.diandian.backend.store.service;

import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.store.domain.StoreOfflineStoreInfoTree;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.mapper.StoreOfflineStoreInfoTreeMapper;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoTreeDetailResp;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoTreePageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoTreeListResp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StoreOfflineStoreInfoTreeService {

    @Autowired
    private StoreOfflineStoreInfoTreeMapper storeOfflineStoreInfoTreeMapper;

    public PageResult page(StoreOfflineStoreInfoTreePageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = storeOfflineStoreInfoTreeMapper.getCount(requestDTO);

        // 查询数据集合
        List<StoreOfflineStoreInfoTree> dataList = storeOfflineStoreInfoTreeMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<StoreOfflineStoreInfoTreePageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreOfflineStoreInfoTreePageResp item = new StoreOfflineStoreInfoTreePageResp();
                item.setId(x.getId());
                item.setNodeCode(x.getNodeCode());
                item.setStoreCode(x.getStoreCode());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<StoreOfflineStoreInfoTreeListResp> getList(StoreOfflineStoreInfoTreeListParamDTO requestDTO){

        // 查询数据集合
        List<StoreOfflineStoreInfoTree> dataList = storeOfflineStoreInfoTreeMapper.getList(requestDTO);

        // 组装数据
        List<StoreOfflineStoreInfoTreeListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreOfflineStoreInfoTreeListResp item = new StoreOfflineStoreInfoTreeListResp();
                item.setId(x.getId());
                item.setNodeCode(x.getNodeCode());
                item.setStoreCode(x.getStoreCode());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public StoreOfflineStoreInfoTreeDetailResp getDetail(StoreOfflineStoreInfoTreeDetailParamDTO param) {

        StoreOfflineStoreInfoTree dbData = storeOfflineStoreInfoTreeMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        StoreOfflineStoreInfoTreeDetailResp result = new StoreOfflineStoreInfoTreeDetailResp();
        result.setId(dbData.getId());
        result.setNodeCode(dbData.getNodeCode());
        result.setStoreCode(dbData.getStoreCode());
        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public StoreOfflineStoreInfoTreeDetailResp getNodeCodeByStoreCode(String belong, String orgId, String orgCode, String storeCode) {


        StoreOfflineStoreInfoTreeDetailParamDTO param = new StoreOfflineStoreInfoTreeDetailParamDTO();
        param.setStoreCode(storeCode);
        param.setBelong(belong);
        param.setOrgCode(orgCode);
        param.setOrgId(orgId);
        StoreOfflineStoreInfoTree dbData = storeOfflineStoreInfoTreeMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        StoreOfflineStoreInfoTreeDetailResp result = new StoreOfflineStoreInfoTreeDetailResp();
        result.setId(dbData.getId());
        result.setNodeCode(dbData.getNodeCode());
        result.setStoreCode(dbData.getStoreCode());
        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(StoreOfflineStoreInfoTreeUpdateByIdParamDTO param) {
        param.check();

        StoreOfflineStoreInfoTreeDetailParamDTO queryDetailDTO = new StoreOfflineStoreInfoTreeDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        StoreOfflineStoreInfoTree dbData = storeOfflineStoreInfoTreeMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        storeOfflineStoreInfoTreeMapper.updateById(param);
    }

    public void updateRelation(StoreOfflineStoreInfoTreeUpdateByIdParamDTO param) {
        param.check();
        storeOfflineStoreInfoTreeMapper.updateRelation(param);
    }

    public void deleteById(StoreOfflineStoreInfoTreeDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        storeOfflineStoreInfoTreeMapper.deleteById(paramDTO);
    }

    public void deleteByStoreCode(String belong, String orgId, String orgCode, String storeCode) {
        storeOfflineStoreInfoTreeMapper.deleteByStoreCode(belong, orgId, orgCode, storeCode);
    }

    public void remove(String belong, String orgId, String orgCode, String nodeCode, String storeCode) {
        storeOfflineStoreInfoTreeMapper.remove(belong, orgId, orgCode, nodeCode, storeCode);
    }

    public void add(StoreOfflineStoreInfoTreeAddParamDTO param) {
        param.setCreateTime(new Date());
        param.setDel(0);
        storeOfflineStoreInfoTreeMapper.add(param);
    }
}
